package com.springboot.cloud.demos.feign.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.demos.entity.enums.ProfitRecordStatus;
import com.springboot.cloud.demos.entity.enums.ProfitRecordType;
import com.springboot.cloud.demos.entity.form.ProfitRecordForm;
import com.springboot.cloud.demos.entity.form.ProfitRecordQueryForm;
import com.springboot.cloud.demos.entity.param.ProfitRecordQueryParam;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.feign.service.ProfitRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class ProfitRecordController extends BaseController {

    @Autowired
    private ProfitRecordService profitRecordService;


    @RequestMapping(value = "/profitRecord/list", method = RequestMethod.GET)
    public Result list(HttpServletRequest request, @RequestParam String type) {
        User user = getUser(request);
        ProfitRecordQueryForm param = new ProfitRecordQueryForm();
        param.setUserId(user.getId());
        param.setStatus(ProfitRecordStatus.pay.getCode());
        param.setType(type);
        Result result = profitRecordService.list(param);
        return result;
    }

    @RequestMapping(value = "/profitRecord/{id}", method = RequestMethod.GET)
    public Result get(@PathVariable(value = "id") String id) {
        return profitRecordService.get(id);
    }


    @RequestMapping(value = "/profitRecord", method = RequestMethod.POST)
    public Result add(HttpServletRequest request,Integer amount) {
        User user= getUser(request);
        ProfitRecordForm profitRecordForm=new ProfitRecordForm();
        profitRecordForm.setUserId(user.getId());
        profitRecordForm.setAmount(amount);
        profitRecordForm.setType(ProfitRecordType.outcome.getCode());
        return profitRecordService.save(profitRecordForm);
    }

}
